package org.sosie2.client.authentication;

import java.io.IOException;
import java.util.*;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.ws.security.WSPasswordCallback;

/**
 * 
 * @author gabriela
 * Is used by the WSSecurityEngine to check the validity of a username-password pair 
 */
public class SimpleCallbackHandler implements CallbackHandler {
	/**
	 * This function is called to handle the callbacks.
	 */
	public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException 
	{

	       for (int i = 0; i < callbacks.length; i++) 
	       {
	           if (callbacks[i] instanceof WSPasswordCallback) 
	           {
	               WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];
	               // set the password given a username
	               if ("gabi".equals(pc.getIdentifer())) 
	               {
	                   pc.setPassword("geheim");
	               }
	           }
	           else 
	           {
	               throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
	           }
	       }
	}
}
